package com.itheima.w_util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;

import java.io.File;
import java.util.List;
import java.util.stream.Collectors;

public class FileUtils {

    public static void createDirectory(String path) {
        if (!FileUtil.exist(path)) {
            FileUtil.mkdir(path);
        }
    }

    public static String joinPath(List<String> paths) {
        return paths.stream().collect(Collectors.joining(File.separator));
    }

    public static void createFile(String startClassPath) {
        if (!FileUtil.exist(startClassPath)) {
            FileUtil.touch(startClassPath);
        }
    }
    public static boolean copyFile(File source, File target,boolean isOverride) {
        if (FileUtil.exist(target)) {
            FileUtil.mkParentDirs(target);
        }
        FileUtil.copy(source, target, isOverride);
        return true;
    }

    public static void formatPomFile(File filePath) {
        List<String> contentList = FileUtil.readLines(filePath, CharsetUtil.CHARSET_UTF_8);
        String content = contentList.stream().collect(Collectors.joining("\n"));
        content=StrUtil.replace(content,"</version>\n\n","</version>\n");
        FileUtil.writeString(content,filePath, CharsetUtil.CHARSET_UTF_8);
    }


}
